/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ddnsclient;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;

/**
 *
 * @author dvtrung
 */
public class AutoUpdate implements Runnable{

    Thread t;
    private JLabel labelTimeRemaining,
            labelStatus;
    private int Secs = 180;
    private JComboBox comboHostName,
            comboCard;
    private boolean ischange = false;
    private String oldIP;
    private JButton btnRefesh;


    public AutoUpdate(JLabel labelTime, JLabel labelStatus,
            JComboBox comboHost, JComboBox combocard, JButton btnRefesh,
            int secs)
    {
        labelTimeRemaining = labelTime;
        this.labelStatus = labelStatus;
        this.comboCard = combocard;
        this.comboHostName = comboHost;
        this.btnRefesh = btnRefesh;
        oldIP = Main.newIP;

        if(secs <= 12)
        {
            Secs = 20;
        }
        else
        {
            Secs = secs;
        }
        t = new Thread(this);
    }


    public void run()
    {
       try
       {
           for(int i = Secs; i >= 0; i--)
           {
               Thread.sleep(1000);
               labelTimeRemaining.setText( String.valueOf(i) + " secs remaining...");
               if(i==Secs - 2)
               {
                   labelStatus.setText(Main.Ethernet + ": " + Main.newIP);
               }
               if(i == 13)
               {
                   labelStatus.setText("Checking IP for " + Main.Ethernet + "...");
                   comboCard.setEnabled(false);
                   comboHostName.setEnabled(false);
                   btnRefesh.setEnabled(false);
               }

               if(i == 9)
               {
                   String newIP = JProcess.getCurrentIP(Main.Ethernet);

                   if(oldIP.equals(newIP))
                   {
                        labelStatus.setText("IP is not change.");
                        comboCard.setEnabled(true);
                        comboHostName.setEnabled(true);
                        btnRefesh.setEnabled(true);
                        i=Secs;
                   }
                    else
                   {
                       Main.newIP = newIP;
                       oldIP = Main.newIP;
                    }
               }
               if(i == 8)
               {
                   labelStatus.setText(Main.Ethernet + ": " + Main.newIP);
               }
               if(i==5)
               {
                   labelStatus.setText("Preparing to update " + Main.Ethernet + ": " + Main.newIP);
               }
               if(i==2)
               {

                   MySendingInformation msi = new MySendingInformation();
                   if(msi.Send())
                   {
                       labelStatus.setText("Update successful.");
                   }
                else
                   {
                        labelStatus.setText("Update failed.");
                   }
               }
               if(i == 0)
               {
                   labelStatus.setText(Main.Ethernet + ": " + Main.newIP);
                   comboCard.setEnabled(true);
                   comboHostName.setEnabled(true);
                   btnRefesh.setEnabled(true);
                   i=Secs;
               }
           }
        }
       catch(InterruptedException ex)
        {

        }
    }

}
